<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace app\daili\controller;
use app\daili\logic\SaleLogic;
use think\Loader;
use think\Page;
use think\Db;

class Sale extends Base {
    /**
     *  拍卖商品列表
    */
    public function index()
    {
        $uid = session('daili_id');
        $keyword = I('keyword');
        if(!empty(I('state'))){
            if(I('state') == 'shen'){
                $msg = "";
            }elseif(I('state') == '2'){
                $msg['state'] = 1;
            }elseif(I('state') == '3'){
                $msg['state'] = 0;
            }else{
                $msg['state'] = 2;
            }
        }else{
            $msg = "";
        }
        $count =  Db::name("sale")->alias('a')->join('sale_cate b','a.parent_id_path = b.parent_id_path')->field('b.name,a.*')->where('a.sname','like',"%".$keyword."%")->where($msg)->where('uid',$uid)->count();
        $Page = $pager = new Page($count,10);    
        $cate = Db::name("sale")->alias('a')->join('sale_cate b','a.parent_id_path = b.parent_id_path')->field('b.name,a.*')->where('a.sname','like',"%".$keyword."%")->where($msg)->where('uid',$uid)->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        $show  = $Page->show();   
        $this->assign('cate',$cate);   
        $this->assign('pager',$pager);
        $this->assign('show',$show);
        return $this->fetch();
    }

    public function addeditsale()
    {       
        $cat_list = M('sale_cate')->where("parent_id = 0")->select(); // 已经改成联动菜单
        $this->assign('cat_list',$cat_list);                
        return $this->fetch(); 
    }

    public function editsale(){
        $id = I('id');
        $cat_list = M('sale_cate')->where("parent_id = 0")->select(); // 已经改成联动菜单
        $brand = M("sale")->find($id); 
        if(IS_POST){
            $data = input('post.');
            $msg = DB::name('sale')->field('id')->where('sname',$data['sname'])->find();
            if($data['id'] != $msg['id'] && $msg){
                $return_arr = array('status' => -1,'msg'   => '已经存在该商品名称','data'  => '');
                $this->ajaxReturn($return_arr); 
            }  
            $validate = Loader::validate('Editsale');// 数据验证
            if(!$validate->batch()->check($data)){
                $error = $validate->getError();
                $error_msg = array_values($error);
                $return_arr = ['status' => 0, 'msg' => $error_msg[0], 'result' => $error];
               $this->ajaxReturn($return_arr);
            }else{
                $pid = DB::name('sale_cate')->field('parent_id_path')->where(id,$data['parent_cat_id'])->find();
                if($data['cat_id'] == 0 && $data['cat_ids'] ==0){
                    $data['parent_id_path'] =  $pid['parent_id_path'];
                }elseif($data['cat_id'] != 0 && $data['cat_ids'] ==0){
                    $data['parent_id_path'] = $pid['parent_id_path'].'_'.$data['cat_id'];
                }else{
                    $data['parent_id_path'] = $pid['parent_id_path'].'_'.$data['cat_id'].'_'.$data['cat_ids'];
                }
                $cateid = explode('_', $data['parent_id_path']);
                $data['catid'] = array_pop($cateid);
                $data['starttime'] = strtotime($data['starttime']);
                $data['endtime'] =  strtotime($data['endtime']);
                if($brand['shop_price'] == $brand['now_price']){    
                    $data['now_price'] = $data['shop_price'];
                }
                $info = DB::name('sale')->update($data);
                if($info){
                    $return_arr = array(
                        'status' => 1,
                        'msg'   => '操作成功',
                        'data'  => array('url'=>U('Daili/sale/index')),
                    );
                    $this->ajaxReturn($return_arr);
                }else{
                    $return_arr = array('status' => -1,'msg'   => '操作失败','data'  => '');
                    $this->ajaxReturn($return_arr);    
                }
            }
        }
        $this->assign('cat_list',$cat_list);      
        $this->assign('brand',$brand);       
        return $this->fetch(); 
    }


    /**
     * 添加和编辑拍卖商品
    */
    public function save()
    {
        $data = input('post.');
        $validate = Loader::validate('Sale');// 数据验证
        if (!$validate->batch()->check($data)) {
            $error = $validate->getError();
            $error_msg = array_values($error);
            $return_arr = ['status' => 0, 'msg' => $error_msg[0], 'result' => $error];
            $this->ajaxReturn($return_arr);
        }else{
            $pid = DB::name('sale_cate')->field('parent_id_path')->where(id,$data['parent_cat_id'])->find();
            if($data['cat_id'] == 0 && $data['cat_ids'] ==0){
                $data['parent_id_path'] =  $pid['parent_id_path'];
            }elseif($data['cat_id'] != 0 && $data['cat_ids'] ==0){
                $data['parent_id_path'] = $pid['parent_id_path'].'_'.$data['cat_id'];
            }else{
                $data['parent_id_path'] = $pid['parent_id_path'].'_'.$data['cat_id'].'_'.$data['cat_ids'];
            }
            $cateid = explode('_', $data['parent_id_path']);
            $data['catid'] = array_pop($cateid);
            $data['starttime'] = strtotime($data['starttime']);
            $data['endtime'] =  strtotime($data['endtime']);
            $data['addtime'] = date('Y-m-d H:i:s');
            $data['uid'] = session('daili_id');
            $data['now_price'] = $data['shop_price'];
            $info = DB::name('sale')->insertGetId($data);
            if(count($data['goods_images']) != 1){
                $msg = array_pop($data['goods_images']);
                foreach ($data['goods_images']  as $key => $val){
                   $msgs['uid'] = $data['uid'];
                   $msgs['goods_id'] = $info;
                   $msgs['image_url'] = $val;
                   DB::name('sale_images')->insert($msgs);
                }
            }
            if($info){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'data'  => array('url'=>U('Daili/sale/index')),
                );
                $this->ajaxReturn($return_arr);
            }else{
                $return_arr = array('status' => -1,'msg'   => '操作失败','data'  => '');
                $this->ajaxReturn($return_arr);    
            }
        }
    }

    /**
     * 添加和编辑拍卖商品
    */
    public function photo(){
        $id = I('id');
        $uid = session('daili_id');
        if($id){
            $image = DB::name('sale')->field('id')->where('id',$id)->find();
            $images = DB::name('sale_images')->where('goods_id',$image['id'])->select();
            $this->assign('image',$image);  
            $this->assign('images',$images);        
        }
        if(IS_POST){
            $data = input('post.');
            $msg = array_pop($data['goods_images']);
            foreach ($data['goods_images']  as $key => $val){
               $msgs['uid'] = $uid;
               $msgs['goods_id'] = $data['id'];
               $msgs['image_url'] = $val;
               DB::name('sale_images')->insert($msgs);
            }
            $this->ajaxReturn(['status' => 1, 'msg' => '操作成功']);

        }
        return $this->fetch();
    }

    /**
     * 删除商品相册图
     */
    public function del_goods_images()
    {
        $path = I('filename','');
        M('sale_images')->where("image_url = '$path'")->delete();
    }

    /**
     * 拍卖分类
    */
    public function get_category()
    {
        $parent_id = I('get.parent_id/d'); // 商品分类 父id
        $list = M('sale_cate')->where("parent_id", $parent_id)->select();
        if ($list) {
            $this->ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $list]);
        }
        $this->ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'result' =>[]]);
    }

    /**
     * 删除分类
     */
    public function delsale()
    {
        $ids = I('post.ids','');
        empty($ids) && $this->ajaxReturn(['status' => -1,'msg' => '非法操作！']);
        $cids = rtrim($ids,",");
        $res=Db::name('sale')->whereIn('id',$cids)->delete();
        if($res){
            $this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>U("Daili/sale/index")]);
        }
        $this->ajaxReturn(['status' => -1,'msg' => '操作失败','data'  =>'']);
    } 

    /**
     * 查看订单详情
     */
    public function order()
    {
       return $this->fetch();
    }     
}